package org.cid54.bukkit.command;

import org.cid54.bukkit.command.util.PlayerCommand;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;
import org.cid54.bukkit.model.Cid54Gate;
import org.cid54.bukkit.model.GateDao;

/**
 *
 * @author Cid54
 */
public class GateCommand implements PlayerCommand {

    private GateDao gateDao = GateDao.instance;

    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {

        String[] commands = command.split(" ");

        if (commands.length > 1) {

            String mode = commands[1].toLowerCase();

            if (mode.equals("list")) {

                int i = 0;

                String text = "";

                for (Cid54Gate gate : this.gateDao.getAllGates()) {

                    text += gate.getName() + " ";
                    i++;

                    if (i % 4 == 0) {

                        event.getPlayer().sendMessage(text);

                        text = "";
                    }

                }


            }

            if (mode.equals("add")) {

                String name = commands[2];

                Cid54Gate gate = new Cid54Gate();
                gate.setName(name);

                Location loc = event.getPlayer().getLocation();

                gate.setX((int) loc.getX());
                gate.setY((int) loc.getY());
                gate.setZ((int) loc.getZ());

                this.gateDao.addGate(gate);

            }

            if (mode.equals("delete")) {

                String name = commands[2];

                Cid54Gate gate = this.gateDao.getGateByName(name);

                if (gate != null) {

                    this.gateDao.deleteGate(gate);

                }

            }
            return true;
        }
        return false;

    }

    public String getCommand() {
        return "gate";
    }

    public String getHelpText() {
        return "gate list|add <name>|delete <name>";
    }
}
